package com.sdruixinggroup.mondayb2b.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.adapter.MessageNoticeAdapter;
import com.sdruixinggroup.mondayb2b.adapter.QuickAdapter;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.Message;
import com.sdruixinggroup.mondayb2b.models.ModelBeen;
import com.sdruixinggroup.mondayb2b.ui.Activities.SystemMessageActivity;
import com.sdruixinggroup.mondayb2b.ui.MessageOneActivity;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import okhttp3.Call;

/**
 * Created by 00029935 on 2017/11/17.
 */

public class MessageNoticeListFragment extends BaseFragment {


    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    LFRecyclerView recyclerView;
    @BindView(R.id.loading)
    ProgressBar loading;
    @BindView(R.id.ib_setting)
    ImageView ibSetting;
    private MessageNoticeAdapter adapter;
    @Override
    protected int getInflateLayout() {
        return R.layout.common_fragment_layout;
    }

    @Override
    protected void setUpView(View view) {
        tvTitle.setText("消息通知");
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        recyclerView.setLoadMore(false);
        recyclerView.setRefresh(true);
        adapter = new MessageNoticeAdapter(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLFRecyclerViewListener(new LFRecyclerView.LFRecyclerViewListener(){
            @Override
            public void onRefresh() {
                queryData(true);
            }

            @Override
            public void onLoadMore() {

            }
        });

        adapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Message.CategoryBean>(){
            @Override
            public void OnItemClick(Message.CategoryBean data, int postion) {
                //type (string, optional):
                //消息类型: 1农资贷 2优惠券 3订单消息 4系统消息

                if(data.getType() == 4){
                    Intent intent3 = new Intent(getContext(),SystemMessageActivity.class);
                    intent3.putExtra("object",data);
                    startActivity(intent3);
                }else {
                    Intent it = new Intent(getContext(),MessageOneActivity.class);
                    it.putExtra("object",data);
                    startActivity(it);
                }
                EventBus.getDefault().post(123);
            }
        });


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.ib_setting)
    public void onClick() {
        getActivity().finish();
    }

    private void queryData(final Boolean isReflush){
        if(isFirstIn ){
            isFirstIn = false;
            loading.setVisibility(View.VISIBLE);
        }

        String url = API.MESSAGE_CATEGORY+"&access_token="+ UserInfoUtil.getInfoToken(getContext());
        HashMap<String, String> heades = new HashMap<>();
        heades.put("Accept", "*/*");

        TypeToken<Message> typeToken = new TypeToken<Message>() {
        };

        OkHttpUtils.get().url(url).headers(heades).build().execute(new ObjectCallBack<Message>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {
                if(null != loading){
                    loading.setVisibility(View.GONE);
                }

                if(null != recyclerView){
                    recyclerView.stopLoadMore();
                    recyclerView.stopRefresh(true);
                }
            }

            @Override
            public void onResponse(Message response, int id) {


                if (null != response) {
                    if (response.getErr_code() == 0) {
                        List<Message.CategoryBean> dataBeen= response.getCategory();
                        if(null != dataBeen && !dataBeen.isEmpty()){
                            adapter.reflashData(dataBeen);
                        }

                    } else {
                        showMsgToast(response.getErr_msg());
                    }
                }

                if(null != loading){
                    loading.setVisibility(View.GONE);
                }

                if(null != recyclerView){
                    recyclerView.stopLoadMore();
                    recyclerView.stopRefresh(true);
                }
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        queryData(true);
    }
}
